package com.study.d024chart;

import java.util.ArrayList;
import java.util.Random;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/** 实现动态折线图
*@description
*@author xtayhame
*date 2020年3月18日
*version
*since v1.0
*/
public class LineChartCustomerMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		HBox hBox = new HBox(20);
		Button monitButton = new Button("监控");
		Button cancelButton = new Button("暂停");


		NumberAxis x = new NumberAxis(0,20,1);
		x.setLabel("时间序列");
		x.setTickUnit(1);
		NumberAxis y = new NumberAxis("值", 0, 100, 10);
		LineChart<Number,Number>  lineChart = new LineChart<>(x, y);
		XYChart.Series<Number, Number> xy1 =new XYChart.Series<Number, Number>();
		xy1.setName("串口一");
		XYChart.Series<Number, Number> xy2= new  XYChart.Series<Number, Number>();
		xy2.setName("串口二");
		lineChart.getData().addAll(xy1,xy2);
		lineChart.setTitle("获取串口数据");
		//去掉动画，要不然耗费CPU
		lineChart.setAnimated(false);

		lineChart.setPrefSize(470, 400);

		hBox.getChildren().addAll(monitButton,cancelButton);
		anchorPane.getChildren().addAll(hBox, lineChart);

		AnchorPane.setTopAnchor(hBox, 20.0);
		AnchorPane.setLeftAnchor(hBox, 20.0);

		AnchorPane.setTopAnchor(lineChart, 40.0);
		AnchorPane.setLeftAnchor(lineChart, 20.0);

		stage.setScene(new Scene(anchorPane, 600, 500));
		stage.show();

		MyScheduleService service = new MyScheduleService();
		service.setDelay(Duration.seconds(0));
		service.setPeriod(Duration.seconds(1));

		//开始监控
		monitButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				if(!service.isRunning()){
					service.start();
				}
			}
		});

		//取消监控
		cancelButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				if(service.isRunning()){
					service.cancel();
					//注意：取消之后，要reset()一下，否则重启会报错
					service.reset();
				}
			}
		});


		service.valueProperty().addListener(new ChangeListener<ArrayList<Integer>>() {

			@Override
			public void changed(ObservableValue<? extends ArrayList<Integer>> observable, ArrayList<Integer> oldValue,
					ArrayList<Integer> newValue) {
				if(newValue !=null){
					 //获取当前的x轴的元素个数
					 int count= xy1.getData().size();

					 //重点： 在这里实现动态效果， x轴随着值变大不断向后移动
					 //x轴初始的时候是20
					 if(count > 18){
						 //分别给x轴的最大值和最小值加1
						 x.setLowerBound(x.getLowerBound() + 1);
						 x.setUpperBound(x.getUpperBound() + 1);
					 }

					//还可以当x轴大于50时，重新来过
					 if(count > 50){
						 //重新设置为
						 count =0;
						 //清空数据
						 xy1.getData().clear();
						 xy2.getData().clear();

						 //重新设置x轴的最大值，最小值
						 x.setLowerBound(0);
						 x.setUpperBound(20);
					 }

					 System.out.println("个数;"+count);
					 XYChart.Data<Number, Number> d1 = new XYChart.Data<Number, Number>(count,newValue.get(0));
					 XYChart.Data<Number, Number> d2 = new XYChart.Data<Number, Number>(count,newValue.get(1));
					 xy1.getData().add(d1);
					 xy2.getData().add(d2);
				}
			}
		});
	}
}

//定时任务， 模拟返回的数据
class MyScheduleService extends ScheduledService<ArrayList<Integer>>{

	@Override
	protected Task<ArrayList<Integer>> createTask() {
		Task<ArrayList<Integer>> task = new Task<ArrayList<Integer>>() {

			@Override
			protected ArrayList<Integer> call() throws Exception {
				ArrayList<Integer>  list = new ArrayList<Integer>();
				Random random = new Random();
				Integer value1 = random.nextInt(100);
				Integer value2 = random.nextInt(100);

				list.add(value1);
				list.add(value2);

				System.out.println("随机数："+value1 +" - " + value2);
				return list;
			}
		};

		return task;
	}


}
